package com.yiqi.liebang.common.rongim.msg;

import android.os.Parcel;
import android.text.TextUtils;

import com.suozhang.framework.utils.logger.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;


@MessageTag(value = "APP:SimpleMsg", flag = MessageTag.ISCOUNTED | MessageTag.ISPERSISTED)
public class QuestionMessage extends MessageContent {


    private static final String TAG = "CustomizeMessage";
    public static final String CONTACT_OPERATION_REQUEST = "Request";
    public static final String CONTACT_OPERATION_ACCEPT_RESPONSE = "AcceptResponse";
    public static final String CONTACT_OPERATION_REJECT_RESPONSE = "RejectResponse";
    private String operation;
    private String sourceUserId;
    private String targetUserId;
    private String extra;
    private String shareUid;
    private String content;
    private String detailType;
    private String detailUid;





    public QuestionMessage() {

    }


    public static QuestionMessage obtain(
            String operation,
            String sourceUserId,
            String targetUserId,
            String extra,
            String shareUid,
            String content,
            String detailType,
            String  detailUid


    ) {
        QuestionMessage obj = new QuestionMessage();
        obj.operation = operation;
        obj.sourceUserId = sourceUserId;
        obj.targetUserId = targetUserId;
        obj.extra = extra;
        obj.shareUid = shareUid;
        obj.content = content;
        obj.detailType=detailType;
        obj.detailUid=detailUid;
        return obj;
    }


    public static final Creator<QuestionMessage> CREATOR = new Creator<QuestionMessage>() {
        public QuestionMessage createFromParcel(Parcel in) {
            return new QuestionMessage(in);
        }

        public QuestionMessage[] newArray(int size) {
            return new QuestionMessage[size];
        }
    };

    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();

        try {
            jsonObj.putOpt("operation", this.operation);
            jsonObj.putOpt("sourceUserId", this.sourceUserId);
            jsonObj.putOpt("targetUserId", this.targetUserId);
            if (!TextUtils.isEmpty(this.getExtra())) {
                jsonObj.putOpt("extra", this.getExtra());
            }



            if (!TextUtils.isEmpty(this.getShareUid())) {
                jsonObj.putOpt("shareUid", this.getShareUid());
            }

            if (!TextUtils.isEmpty(this.getContent())) {
                jsonObj.putOpt("content", this.getContent());
            }
            if (!TextUtils.isEmpty(this.getDetailType())) {
                jsonObj.putOpt("detailType", this.getDetailType());
            }

            if (!TextUtils.isEmpty(this.getDetailUid())) {
                jsonObj.putOpt("detailUid", this.getDetailUid());
            }
            if (this.getJSONUserInfo() != null) {
                jsonObj.putOpt("user", this.getJSONUserInfo());
            }


        } catch (JSONException var4) {
            RLog.e("ContactNotificationMessage", "JSONException " + var4.getMessage());
        }

        try {
            return jsonObj.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public String getDetailUid() {
        return detailUid;
    }

    public void setDetailUid(String detailUid) {
        this.detailUid = detailUid;
    }

    public QuestionMessage(byte[] data) {
        String jsonStr = null;

        try {
            jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException var5) {
            var5.printStackTrace();
        }

        try {
            JSONObject jsonObj = new JSONObject(jsonStr);
            this.setOperation(jsonObj.optString("operation"));
            this.setSourceUserId(jsonObj.optString("sourceUserId"));
            this.setTargetUserId(jsonObj.optString("targetUserId"));
            this.setExtra(jsonObj.optString("extra"));
            this.setShareUid(jsonObj.optString("shareUid"));
            this.setContent(jsonObj.optString("content"));
            this.setDetailType(jsonObj.optString("detailType"));
            this.setDetailUid(jsonObj.optString("detailUid"));


            if (jsonObj.has("user")) {
                this.setUserInfo(this.parseJsonToUserInfo(jsonObj.getJSONObject("user")));
            }
        } catch (JSONException var4) {
            RLog.e("CustomizeMessage", "JSONException " + var4.getMessage());
        }

    }

    public QuestionMessage(Parcel in) {
        this.operation = ParcelUtils.readFromParcel(in);
        this.sourceUserId = ParcelUtils.readFromParcel(in);
        this.targetUserId = ParcelUtils.readFromParcel(in);
        this.extra = ParcelUtils.readFromParcel(in);
        this.shareUid = ParcelUtils.readFromParcel(in);
        this.content = ParcelUtils.readFromParcel(in);
        this.detailType=ParcelUtils.readFromParcel(in);
        this.detailUid=ParcelUtils.readFromParcel(in);


        try {
            this.setUserInfo(ParcelUtils.readFromParcel(in, UserInfo.class));
        } catch (Exception e) {
            Logger.e("Exception-----Msg>" + e.getMessage());
        }


    }

    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, this.operation);
        ParcelUtils.writeToParcel(dest, this.sourceUserId);
        ParcelUtils.writeToParcel(dest, this.targetUserId);
        ParcelUtils.writeToParcel(dest, this.extra);
        ParcelUtils.writeToParcel(dest, this.shareUid);
        ParcelUtils.writeToParcel(dest, this.content);
        ParcelUtils.writeToParcel(dest, this.detailType);
        ParcelUtils.writeToParcel(dest, this.detailUid);



        try {
            ParcelUtils.writeToParcel(dest, getUserInfo());
        } catch (Exception e) {
            Logger.e("Exception-----Msg>" + e.getMessage());
        }

    }

    public int describeContents() {
        return 0;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public String getSourceUserId() {
        return sourceUserId;
    }

    public void setSourceUserId(String sourceUserId) {
        this.sourceUserId = sourceUserId;
    }

    public String getTargetUserId() {
        return targetUserId;
    }

    public void setTargetUserId(String targetUserId) {
        this.targetUserId = targetUserId;
    }

    public String getExtra() {
        return extra;
    }

    public void setExtra(String extra) {
        this.extra = extra;
    }


    public String getShareUid() {
        return shareUid;
    }

    public void setShareUid(String shareUid) {
        this.shareUid = shareUid;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getDetailType() {
        return detailType;
    }

    public void setDetailType(String detailType) {
        this.detailType = detailType;
    }
}